//package com.ytjj.quartz.task;
//
//import com.alibaba.fastjson.JSONObject;
//import okhttp3.*;
//import com.ytjj.common.constants.RedisConstants;
//import com.ytjj.common.service.RedisService;
//import com.ytjj.qmyx.mall.mapper.OrdersMapper;
//import com.ytjj.qmyx.mall.model.Orders;
//import com.ytjj.qmyx.users.mapper.UsersActiveMapper;
//import com.ytjj.qmyx.users.model.UsersActive;
//import com.ytjj.qmyx.users.model.UsersActiveExample;
//import com.ytjj.quartz.dao.UsersAnalysisDao;
//import com.ytjj.quartz.utils.DingDingWebHookUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.UUID;
//import java.util.stream.Collectors;
//
///**
// *
// * @version 1.0
// *
// */
//@Component
//@Slf4j
//public class YybReportTask {
//
//    @Resource
//    private UsersAnalysisDao usersAnalysisDao;
//    @Resource
//    private OrdersMapper ordersMapper;
//    @Autowired
//    private RedisService redisService;
//
////    @Scheduled(cron = "0 0/5 * * * ?")
////    public void updateUsersAnalysis() {
////        log.info("===== 开始上报腾讯云广告数据 =====");
////        List<UsersActive> usersActiveList = usersAnalysisDao.queryNotYybReportList();
////        log.info("===== 结束上报腾讯云广告数据 =====");
////    }
//
//    @Scheduled(cron = "0 0/5 * * * ?")
//    public void reportPayOrderToVivo() {
//        String channel = "vivo";
//        List<Orders> payOrderList = ordersMapper.queryPayOrderByVivoList(channel, 5, "h5");
//        if (CollectionUtils.isEmpty(payOrderList)) {
//            return;
//        }
//        log.info("===reportPayOrderToVivo.list.size()==={}", payOrderList.size());
//        int forNum = payOrderList.size() / 100;
//        if (payOrderList.size() % 100 > 0) {
//            forNum = forNum + 1;
//        }
//        for (int i = 0; i < forNum; i++) {
//            List<Orders> newList = null;
//            if (i == forNum - 1) {
//                newList = payOrderList.subList(i * 100, payOrderList.size());
//            } else {
//                newList = payOrderList.subList(i * 100, i * 100 + 100);
//            }
//            try {
//                String requestJsonStr = generateRequestJSONStr(newList);
//                handle(requestJsonStr);
//            } catch (IOException e) {
//                log.info("===推送vivo商业开放平台异常订单数据:{}", JSONObject.toJSONString(newList.stream().map(Orders::getId).collect(Collectors.toSet())));
//                log.info("===推送vivo商业开放平台异常==={}", e);
//            }
//        }
//    }
//
//    private static final String template = "<<<%s>>> \n 【服务名】: %s(%s) \n 【状态】: %s(%s) \n 【服务ip】: %s \n 【详情】: %s";
//
//    private void handle(String requestJsonStr) throws IOException {
//        log.info("===handle==={}", requestJsonStr);
//        String url = "https://marketing-api.vivo.com.cn/openapi/v1/advertiser/behavior/upload";
//        String advertiserId = "22477a534a2591562db6";// 账户ID
//        String nonce = UUID.randomUUID().toString().replace("-", "");// 随机字串标识，不超过32个字符
//        String accessToken = getAccessToken();
//        if (StringUtils.isEmpty(accessToken)) {
//            String messageText = String
//                    .format(template, "订单上报vivo平台失败",
//                            "pengke-quartz", "schedule",
//                            "500", "订单上报VIVO",
//                            "",
//                            JSONObject.toJSONString("XXXX优品支付订单上报VIVO失败：ACCESS_TOKEN失效了"));
//            DingDingWebHookUtil.pushInfoToDingding(messageText);
//            return;
//        }
//        String timestamp = String.valueOf(System.currentTimeMillis());
//        OkHttpClient client = new OkHttpClient().newBuilder().build();
//        MediaType mediaType = MediaType.parse("application/json");
//        String jsonStr = requestJsonStr; //使用请求参数的json串替换，格式如下方示例
//        RequestBody body = RequestBody.create(mediaType, jsonStr);
//        Request request = new Request.Builder()
//                .url(url + "?access_token=" + accessToken + "&timestamp=" + timestamp + "&nonce=" + nonce + "&advertiser_id=" + advertiserId)
//                .method("POST", body)
//                .addHeader("Content-Type", "application/json")
//                .build();
//        Response response = client.newCall(request).execute();
//        log.info("===上报vivo商业开放平台返回数据==={}", response.body().string());
//    }
//
//    private String generateRequestJSONStr(List<Orders> ordersList) {
//        log.info("===generateRequestJSONStr==={}", ordersList.size());
//        JSONObject outObject = new JSONObject();
//        outObject.put("srcType", "Web");
//        outObject.put("pageUrl", "");
//        outObject.put("srcId", "ds-202208114855");
//        List<Object> list = new ArrayList<>();
//        ordersList.stream().forEach(item -> {
//            JSONObject object = new JSONObject();
//            object.put("cvType", "PAY");
//            object.put("cvTime", item.getCreateTime().getTime());
//            list.add(object);
//        });
//        outObject.put("dataList", list);
//        return outObject.toJSONString();
//    }
//
//    private String getAccessToken() {
//        String accessToken = redisService.get(RedisConstants.VIVOAUTHTOKEN_KEY + "20220811017");
//        if (!StringUtils.isEmpty(accessToken)) {
//            return accessToken;
//        }
//        log.error("===vivo平台access_token失效了");
//        return null;
//    }
//
//}
